package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.bean.BeanPubContent;
import com.lepu.app.fun.my.bean.BeanPubContentImage;

public class AdapterPubContent extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanPubContent> mArrayList = null;
	@SuppressWarnings("unused")
	private String mFromContext;

	public AdapterPubContent(Context context,ArrayList<BeanPubContent> arrayList,String fromContext) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mFromContext = fromContext;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_pub_content_listitem, null);

			holder = new ViewHolder();

			ImageView iconImageView = (ImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView subTitleTextView = (TextView) convertView
					.findViewById(R.id.subTitleTextView);
			TextView pubTimeTextView = (TextView) convertView
					.findViewById(R.id.pubTimeTextView);
			TextView statusTextView = (TextView) convertView
					.findViewById(R.id.statusTextView);
			TextView deleteTextView = (TextView) convertView
					.findViewById(R.id.deleteTextView);
			TextView circleTextView = (TextView) convertView
					.findViewById(R.id.circleTextView);
			TextView pointTextView = (TextView) convertView
					.findViewById(R.id.pointTextView);
			RelativeLayout subTitleLayout = (RelativeLayout) convertView
					.findViewById(R.id.subTitleLayout);

			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.subTitleTextView = subTitleTextView;
			holder.pubTimeTextView = pubTimeTextView;
			holder.statusTextView = statusTextView;
			holder.deleteTextView = deleteTextView;
			holder.circleTextView = circleTextView;
			holder.pointTextView = pointTextView;
			holder.subTitleLayout = subTitleLayout;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanPubContent item = mArrayList.get(position);
		
		if(!TextUtils.isEmpty(item.TopicDetail))
		{
			holder.titleTextView.setText(item.TopicTitle);
			holder.iconImageView.setVisibility(View.GONE);
			holder.subTitleTextView.setText("");
			holder.subTitleLayout.setVisibility(View.GONE);
			
		}else{
			
			holder.subTitleLayout.setVisibility(View.VISIBLE);
			
			if(!TextUtils.isEmpty(item.TopicDescription))
			{
				holder.titleTextView.setText(item.TopicDescription);
			}else{
				holder.titleTextView.setText(item.UserNickName);
			}
			
			if(item.MediaList != null && item.MediaList.size() > 0)
			{
				holder.iconImageView.setVisibility(View.VISIBLE);
				BeanPubContentImage image = item.MediaList.get(0);
				
				if (!TextUtils.isEmpty(image.MediaUri)) {
					String imageUrl = Setting.getImageUrl(image.MediaUri);
					UniversalImageLoadTool.disPlay(imageUrl,
							holder.iconImageView, R.drawable.default_icon_small);
				}
				
				int count = item.MediaList.size();
				holder.subTitleTextView.setText(String.format("共%d张图片", count));
			}else{
				holder.iconImageView.setVisibility(View.GONE);
				holder.subTitleTextView.setText("");
			}
		}
		
		Date date = DateUtilBase.dateFromString(item.RecordCreateTime, DateUtilBase.DATE_ALL);
		holder.pubTimeTextView.setText(DateUtilBase.stringFromDate(date, "yyyy年MM月dd HH:mm"));

		holder.pointTextView.setVisibility(View.VISIBLE);
		if(TextUtils.isEmpty(item.CheckStatus))
		{
			holder.statusTextView.setText("");
		}else{
			if (item.CheckStatus.equals("2")) {
				holder.statusTextView.setText("未通过");
			} else {
				holder.statusTextView.setText("");
				holder.pointTextView.setVisibility(View.GONE);
			}
		}
		
		holder.deleteTextView.setVisibility(View.GONE);
        holder.circleTextView.setText(item.CategoryTitle);
        
        if(item.IsGood == 1){
        	holder.titleTextView.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.drawable.mother_topic_good_icon), null, null, null);
        }else{
        	holder.titleTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
        
		return convertView;
	}

	private static class ViewHolder {

		ImageView iconImageView;
		TextView titleTextView;
		TextView subTitleTextView;
		TextView pubTimeTextView;
		TextView statusTextView;
		TextView deleteTextView;
		TextView circleTextView;
		TextView pointTextView;
		RelativeLayout subTitleLayout;
	}
}
